package com.shcz.migu.widget.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;


public class ImageViewExtends extends AppCompatImageView {
    private int width = 0;
    private int height = 0;
    private Paint paint = new Paint();
    boolean isred = false;
    float radd = 4;
    int redCircleY = -1;
    int radius = -1;
    private float texthei;
    private int xOffset = -1;

    public ImageViewExtends(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setRedDoat(boolean isred) {
        this.isred = isred;
        if (isred) {
            paint.setTextSize(dip2px(15));
            texthei = paint.descent() - paint.ascent();
//            final float scale = getContext().getResources().getDisplayMetrics().density;
//            if(radius == -1){
//                radd = (int)scale * 4;
//            }else{
//                radd = (int)scale * radius;
//            }
            radd = texthei / 6;
        }
        this.invalidate();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
//        return (int) (dpValue * scale + 0.5f);
        return (int) (dpValue * scale);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isred) {
            width = getWidth();
            height = getHeight();
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(0xfff5380a);
            if (redCircleY < 0) {
                canvas.drawCircle(width - radd, (radd * 2), radd, paint);
            } else {
                if (xOffset != -1) {
                    canvas.drawCircle(width * 2 / 3 + radius + xOffset, height / 3 + redCircleY + radd, radd, paint);
                } else
                    canvas.drawCircle(width - radd, redCircleY + radd, radd, paint);
            }
        }

    }

    public void setRedCircleY(int redCircleY) {
        this.redCircleY = redCircleY;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setXoffset(int xOffset) {
        this.xOffset = xOffset;
    }
}
